//
//  UserManager.swift
//  SameCity
//
//  Created by 秦江涛 on 2023/6/14.
//

import UIKit
import CommonCrypto

class UserManager: NSObject {
    
    public static let shared = UserManager()
    
    /// 获取用户信息
    func getUserData(_ completed: voidBlock? = nil) {
        QINetworkManager.request(urlStr: "Users/users_edit", method: .get, parameters: nil) { data in
            print(data)
//            let userData = try? JSONSerialization.data(withJSONObject: data, options: [])
            Defaults[\.userData] = data
            completed?()
        } 
    }
    
}

extension DefaultsKeys {
    var isLogin: DefaultsKey<Bool>{.init("isLogin", defaultValue: false)}
    var token: DefaultsKey<String>{.init("token", defaultValue: "")}
    var bearer: DefaultsKey<String>{.init("bearer", defaultValue: "")}
    var userData: DefaultsKey<Data>{.init("userData", defaultValue: Data())}
}


class QIUserModel: NSObject, Convertible {
//    static var share = QIUserModel()
    
    var id = ""
    var loginip = ""
    var group_id = ""
    var bio = ""
    var nickname = ""
    var mobile = ""
    var avatar = ""
    var mini_openid = ""
    var unionid = ""
    var maxsuccessions = ""
    var email = ""
    var money = ""
    var city = ""
    var wechat_openid = ""
    var gender = ""
    var height = ""
    var password = ""
    var level = ""
    var verification = ""
    var token = ""
    var username = ""
    var status = ""
    var birthday = ""
    var salt = ""
    var createtime = ""
    
    class func shared() -> QIUserModel {
        let userData = Defaults[\.userData]
        let json = JSON(userData)
        let model = json.dictionaryObject?.kj.model(QIUserModel.self) ?? QIUserModel()
        return model
    }
    
    required override init() {}
}




















